define(['server', 'login-reg', 'jquery'], (server, loginres) => {
    class Nav {
        constructor() {
            this.loadNav().then(() => {
                this.navBindEvent()
                this.jumpto()
                $.get('/user/isLogin', resp => {
                    if (resp.code === 200) {
                        $('.yes_login').removeClass('hidden');
                        $('.no_login').addClass('hidden');
                        $('#loginedUser').html(resp.body.username);
                        $('#cancelUser').on('click', () => {
                            $.get(server.baseServerUrl + '/user/unLogin', resp => {
                                location.reload()
                            })
                        })
                    }
                })

                this.calcCartCount()
            });
        }
        loadNav() {
            return new Promise((resolve) => {
                $('.nav ').load('/html/modules/nav.html', () => {
                    resolve()
                })
            })
        }
        navBindEvent() {
            $('#navfouce li').hover(function() {
                $(this).children('.nav-sublist').stop().slideDown()

            }, function() {
                $(this).children('.nav-sublist').stop().slideUp()
            })
            $('.login').load('/html/modules/login-reg.html .login-box', () => {
                //将登陆模块加载到nav导航页面上，这样就不用每个页面都导入登录注册了
            })
            $('.reg').load('/html/modules/login-reg.html .reg-box', () => {
                //将登陆模块加载到nav页面上,这样就不用每个页面都导入登录注册了
            })
            $(".pop-box-btn").on('click', function() {
                loginres.popBoxAlready($(this).attr('flag'));
            })
        }
        loadLoginRes() {
            return new Promise((resolve) => {
                $('.nav .content').load('/html/modules/nav.html', () => {
                    resolve()
                })
            })
        }
        jumpto() {
            $('.mcate-item-bd').on('click', 'a', function() {
                location.href = '/html/category.html?' + $(this).parents('li').attr('class') + '=' + $(this).attr('class-type')

                // open('/html/category.html?' + $(this).parents('li').attr('class') + '=' + $(this).attr('class-type'));
            })
        }
        calcCartCount() {
            let cart = localStorage.getItem('cart')
            let count = 0
            if (cart) {
                cart = JSON.parse(cart)
                count = cart.reduce((sum, shop) => {
                    sum += shop.count
                    return sum
                }, 0)
            }
            $('.no_login .shopcart_num').html(count)
        }
    }
    return new Nav()
})